<?php
/**
 * admin
 * 管理员
 * @author Administrator
 * @version
 */
class Model_Tables_Admin extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'admin';
    
    public function add( $params )
    {
        $data = array(
		'username' => $params['username'],
		'password' => md5($params['password']),
		//'allowmultilogin' => $params['allowmultilogin'],
		'disabled' => $params['disabled'],
		//'lastloginip' => $params['lastloginip'],
		//'lastlogintime' => $params['lastlogintime'],
		//'logintimes' => $params['logintimes'],
		'email' => $params['email'],
        'issp' => $params['issp'],
        'spid' => $params['spid'],
        'roles' => ",".implode($params['roles'],",").",",
        );
        $roles = $params['roles'];//权限数组
        $db = $this->getAdapter();
    	$db->beginTransaction();
		try {
			$this->insert($data);
			$userid = $db->lastInsertId();
			$db->query("delete from admin_role where userid = '$userid'");
			foreach($roles as $role)
			{
				$db->query("insert into admin_role(userid, roleid) values('$userid','$role')");
			}
		    $db->commit();
		} catch (Exception $e) {
		    $db->rollBack();
		    return false;
		}
		return true;
    }
    public function modify( $params, $userid )
    {
        $data = array(
		//'username' => $params['username'],
		'disabled' => $params['disabled'],
		'email' => $params['email'],
        'issp' => $params['issp'],
        'spid' => $params['spid'],
        'roles' => ",".implode($params['roles'],",").",",
        );
        if($params['password'] != "")
        {
        	$data['password'] =md5($params['password']);
        }
        $where = 'userid = '.$userid ;        
        $roles = $params['roles'];//权限数组
        $db = $this->getAdapter();
    	$db->beginTransaction();
		try {
			$this->update($data , $where );
			$db->query("delete from admin_role where userid = '$userid'");
			foreach($roles as $role)
			{
				$db->query("insert into admin_role(userid, roleid) values('$userid','$role')");
			}
		    $db->commit();
		} catch (Exception $e) {
		    $db->rollBack();
		    return false;
		}
		return true;
        
        
        
        //return $this->update($data , $where );
    }
    public function modifyAjax( $userid, $field, $value )
    {
    	$data = array($field => $value);
        $where = 'userid = '.$userid;
        return $this->update($data , $where );
    }
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update admin set  $field =not  $field  where userid=' $pri'";
		return $db->query($sql);
    }
    public function del( $userid )
    {
     	$where = 'userid = '.$userid;
        return $this->delete($where);
    }
    public function getInfo($userid)
    {
        $row = $this->fetchRow('userid = ' . $userid);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    public function getCount($s = "")
    { 
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
			$where = $db->quoteInto("roles like ?", "%,$s,%");
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /*
     * 根据角色名称获取管理员列表
     */
    public function getList($page, $numPerPage = 10,$s= "")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('userid');
        if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("roles like ?", "%,$s,%");
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    
    /**
     * 修改密码
     */
    public function updatepassword($params)
    {
    	$data = array();
        if($params['password'] != "")
        {
        	$data['password'] =md5($params['password1']);
        }
        $where = 'userid = '.$params['userid'] ;
    	try {
			$this->update($data , $where );
		} catch (Exception $e) {
		    return false;
		}
		return true;
    }
    
    /*
     * 检查原始密码输入是否正确
     */
    public function checkpassword($userid,$password)
    {
    	$sql = "select * from admin where userid=".$userid." and password='".md5($password)."'";
    	$result = $this->_db->query($sql)->fetchAll();
    	if($result)
    	{return true;}
    	else
    	{return false;};
    }
}

